import { Controller,Get,UseGuards,Request, Post } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiTags, ApiResponse, ApiBody, ApiHeader,ApiBearerAuth} from '@nestjs/swagger';
import { AuthService } from "../../services/auth/auth.service";
import { UserDto } from '../../responses/user.dto';

@ApiTags('auth')
@Controller('api/auth')
export class AuthController {
  constructor(private readonly authService: AuthService){}
  @UseGuards(AuthGuard('local'))
  @Post('login')
  @ApiResponse({ status: 403, description: 'Forbidden.'})
  @ApiResponse({ status: 200, description: 'Success.'})
  @ApiBody({
    type: UserDto,

  })
  async login(@Request() req){
    const user=req.user;
    user.last_login_ip=req.ip;
    user.last_login_time=(new Date()).toISOString();
    return this.authService.login(user);
  }

  @UseGuards(AuthGuard('jwt'))
  @Get('profile')
  @ApiBearerAuth()
  @ApiResponse({ status: 403, description: 'Forbidden.'})
  @ApiResponse({ status: 200, description: 'Success.'})
  getProfile(@Request() req) {
    return req.user;
  }
}
